package com.teach.demo03;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

import com.teach.Machine;
import com.teach.Product;
import com.teach.TypeName;
import com.teach.exception.NotSufficientFundsException;
import com.teach.exception.ParamLtZeroException;
import com.teach.exception.ProductNotExistsException;

/**
 * 自动售卖机
 * @author J.L.Zhou
 *
 */
public class Machine01 implements Machine {
	
	private List<Product> list = new ArrayList<>();
	
	private long idCount=1;
	
	private double money = 0;
	
	
	/**
	 * 添加一个商品
	 * @param product
	 */
	public void add(Product product){
		product.setId(idCount++);
		list.add(product);
	}
	
	public List<CountResult> list(){
		List<CountResult> ret = new ArrayList<>();
		for(Product p: list){
			CountResult cr = null;
//			for(CountResult c : ret){
//				if(c.getName().equals(p.getName().getTypeName())&&c.getBrand().equals(p.getBrand())){
//					cr = c;
//					break;
//				}
//			}
//			System.out.println(p);
			CountResult search = new CountResult();
			search.setName(p.getName().getTypeName());
			search.setBrand(p.getBrand());
//			System.out.println(ret.indexOf(search));
			int index = ret.indexOf(search);
			if(index>-1){
				cr = ret.get(index);
			}
			
			
			if(cr==null){
				cr = new CountResult();
				cr.setName(p.getName().getTypeName());
				cr.setBrand(p.getBrand());
				cr.setPrice(p.getPrice());
				cr.setNum(1);
				ret.add(cr);
			}else{
				cr.setNum(cr.getNum()+1);
			}
		}
		return ret;
	}

	/**
	 * 
	 * @param name
	 * @param brand
	 * @param price
	 * @throws ParamLtZeroException
	 */
	public void setPrice(TypeName name,String brand,double price)throws ParamLtZeroException{
		if(price<0){
			throw new ParamLtZeroException();
		}
//		list.forEach(new Consumer<Product>() {
//
//			@Override
//			public void accept(Product t) {
//				// TODO Auto-generated method stub
//				
//			}
//		});
		list.forEach(t->{
			if(t.getName()==name&&t.getBrand().equals(brand)){
				t.setPrice(price);
			}
		});//lambda
	}
	
	/**
	 * 投币
	 * @param money
	 * @throws ParamLtZeroException
	 */
	public void addMoney(double money)throws ParamLtZeroException{
		if(money<0){
			throw new ParamLtZeroException();
		}
		this.money += money;
	}
	
	/**
	 * 获取余额
	 * @return
	 */
	public double getMoney(){
		return money;
	}
	
	/**
	 * 找零
	 * @return
	 */
	public double give(){
		double money = this.money;
		this.money=0;
		return money;
	}
	
	/**
	 * 购买商品
	 * @param name
	 * @param brand
	 * @return 具体购买的商品对象
	 * @throws NotSufficientFundsException
	 * @throws ProductNotExistsException
	 */
	public Product buy(TypeName name,String brand)throws NotSufficientFundsException,ProductNotExistsException{
		Product search = new Product(name, brand, null);
		int index = list.indexOf(search);
		if(index==-1){
			throw new ProductNotExistsException();
		}
		Product removeObj = list.get(index);
		if(money<removeObj.getPrice()){
			throw new NotSufficientFundsException();
		}
		money -= removeObj.getPrice();
		list.remove(index);
		return removeObj;
	}

	@Override
	public String toString() {
		return "Machine:"+list.toString();
	}

	@Override
	public List<String> listBrand(TypeName name) {
		Set<String> set = new HashSet<>();
		list.forEach(i->{
			if(i.getName()==name){
				set.add(i.getBrand());
			}
		});
		List<String> ret = new ArrayList<>();
		ret.addAll(set);
		return ret;
	}
	

	
}
